/*
 * Created by JFormDesigner on Thu Jul 08 18:19:54 CST 2021
 */

package com.tarena.view;

import com.tarena.pojo.Message;
import com.tarena.pojo.MessageType;
import com.tarena.pojo.User;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author unknown
 */
public class ChatMainWindow extends JFrame {
    private Socket socket;
    private ObjectOutputStream oos;
    private User targerUser;
    private User sender;
   /* private User currentUser;
    private User targetUser;
    private ObjectOutputStream out;*/

    public ChatMainWindow(Socket socket,ObjectOutputStream oos,User targerUser,User sender) {
            initComponents();
            this.socket = socket;
            this.oos=oos;
            this.targerUser=targerUser;
            this.sender=sender;

    }
    /*显示聊天信息*/
    public void setText(String s){
        textArea1.append(s+"\n");
        textArea2.setText("");
    }

    private void textArea2KeyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isAltDown()) {
            /*1.获取聊天信息*/
            String text = textArea2.getText();
            /*构建message对象用于发送*/
            Message message =new Message();
            message.setMagType(MessageType.MESSAGE_COMM_MES);
            message.setContent(text);
            message.setGetter(targerUser);
            message.setSender(sender);
            /*3.发送聊天信息*/
            try {
                oos.writeObject(message);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            setText("我说： "+text);
        }
    }

    private void label20MouseClicked(MouseEvent e) {
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), "选择");
        File file = jfc.getSelectedFile();
        if (file != null && file.isDirectory()) {
            System.out.println("文件夹:" + file.getAbsolutePath());
        } else if (file != null && file.isFile()) {
            System.out.println("文件:" + file.getAbsolutePath());
        }
    }

    private void label20MousePressed(MouseEvent e) {
        label20.setBackground(Color.WHITE);
    }

    private void label20MouseReleased(MouseEvent e) {
        label20.setBackground(new Color(242, 242, 242));
    }

    private void label21MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void label21MousePressed(MouseEvent e) {
        label21.setBackground(Color.WHITE);
    }

    private void label21MouseReleased(MouseEvent e) {
        label21.setBackground(new Color(242, 242, 242));
    }

    private void label22MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void label22MousePressed(MouseEvent e) {
        label22.setBackground(Color.WHITE);
    }

    private void label22MouseReleased(MouseEvent e) {
        label22.setBackground(new Color(242, 242, 242));
    }

    private void label23MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void label23MousePressed(MouseEvent e) {
        label23.setBackground(Color.WHITE);
    }

    private void label23MouseReleased(MouseEvent e) {
        label23.setBackground(new Color(242, 242, 242));
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        panel1 = new JPanel();
        label8 = new JLabel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        scrollPane2 = new JScrollPane();
        textArea2 = new JTextArea();
        panel7 = new JPanel();
        panel8 = new JPanel();
        panel9 = new JPanel();
        panel10 = new JPanel();
        scrollPane3 = new JScrollPane();
        list1 = new JList();
        panel4 = new JPanel();
        panel6 = new JPanel();
        label24 = new JLabel();
        label20 = new JLabel();
        label21 = new JLabel();
        label22 = new JLabel();
        label23 = new JLabel();
        panel5 = new JPanel();
        scrollPane1 = new JScrollPane();
        textArea1 = new JTextArea();

        //======== this ========
        setMinimumSize(new Dimension(800, 620));
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout(6, 8));

        //======== panel1 ========
        {
            panel1.setMinimumSize(new Dimension(800, 24));
            panel1.setPreferredSize(new Dimension(800, 24));
            panel1.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(
                    0, 0, 0, 0), "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn", TitledBorder.CENTER, TitledBorder
                    .BOTTOM, new Font("Dia\u006cog", Font.BOLD, 12), Color.
                    red), panel1.getBorder()));
            panel1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.
                                                   beans.PropertyChangeEvent e) {
                    if ("\u0062ord\u0065r".equals(e.getPropertyName())) throw new RuntimeException();
                }
            });
            panel1.setLayout(new GridLayout());

            //---- label8 ----
            label8.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));
            panel1.add(label8);
        }
        contentPane.add(panel1, BorderLayout.NORTH);

        //======== panel2 ========
        {
            panel2.setLayout(new GridLayout());
        }
        contentPane.add(panel2, BorderLayout.WEST);

        //======== panel3 ========
        {
            panel3.setMinimumSize(new Dimension(760, 100));
            panel3.setPreferredSize(new Dimension(760, 100));
            panel3.setLayout(new BorderLayout());

            //======== scrollPane2 ========
            {
                scrollPane2.setMinimumSize(new Dimension(760, 80));
                scrollPane2.setPreferredSize(new Dimension(760, 80));
                scrollPane2.setAutoscrolls(true);

                //---- textArea2 ----
                textArea2.setMinimumSize(new Dimension(760, 80));
                textArea2.setPreferredSize(new Dimension(760, 80));
                textArea2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
                textArea2.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        textArea2KeyPressed(e);
                    }
                });
                scrollPane2.setViewportView(textArea2);
            }
            panel3.add(scrollPane2, BorderLayout.CENTER);

            //======== panel7 ========
            {
                panel7.setMinimumSize(new Dimension(10, 0));
                panel7.setPreferredSize(new Dimension(10, 0));
                panel7.setLayout(new GridLayout());
            }
            panel3.add(panel7, BorderLayout.WEST);

            //======== panel8 ========
            {
                panel8.setMinimumSize(new Dimension(10, 0));
                panel8.setPreferredSize(new Dimension(10, 0));
                panel8.setLayout(new GridLayout());
            }
            panel3.add(panel8, BorderLayout.EAST);

            //======== panel9 ========
            {
                panel9.setMinimumSize(new Dimension(0, 10));
                panel9.setPreferredSize(new Dimension(0, 10));
                panel9.setLayout(new GridLayout());
            }
            panel3.add(panel9, BorderLayout.SOUTH);

            //======== panel10 ========
            {
                panel10.setMinimumSize(new Dimension(600, 6));
                panel10.setPreferredSize(new Dimension(600, 164));
                panel10.setVisible(false);
                panel10.setLayout(new GridLayout());

                //======== scrollPane3 ========
                {
                    scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                    scrollPane3.setMinimumSize(new Dimension(600, 100));
                    scrollPane3.setPreferredSize(new Dimension(600, 100));

                    //---- list1 ----
                    list1.setMaximumSize(new Dimension(600, 100));
                    list1.setMinimumSize(new Dimension(600, 100));
                    list1.setPreferredSize(new Dimension(600, 100));
                    list1.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                    list1.setVisibleRowCount(4);
                    scrollPane3.setViewportView(list1);
                }
                panel10.add(scrollPane3);
            }
            panel3.add(panel10, BorderLayout.NORTH);
        }
        contentPane.add(panel3, BorderLayout.SOUTH);

        //======== panel4 ========
        {
            panel4.setLayout(new BorderLayout());

            //======== panel6 ========
            {
                panel6.setMinimumSize(new Dimension(120, 0));
                panel6.setPreferredSize(new Dimension(120, 0));
                panel6.setLayout(new GridLayout(6, 0, 0, 6));

                //---- label24 ----
                label24.setMaximumSize(new Dimension(60, 60));
                label24.setMinimumSize(new Dimension(60, 60));
                label24.setPreferredSize(new Dimension(60, 60));
                label24.setHorizontalAlignment(SwingConstants.CENTER);
                panel6.add(label24);

                //---- label20 ----
                label20.setText("\u53d1\u6587\u4ef6");
                label20.setHorizontalAlignment(SwingConstants.CENTER);
                label20.setMaximumSize(new Dimension(30, 19));
                label20.setMinimumSize(new Dimension(30, 19));
                label20.setPreferredSize(new Dimension(30, 19));
                label20.setBorder(new BevelBorder(BevelBorder.RAISED));
                label20.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label20.setOpaque(true);
                label20.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        label20MouseClicked(e);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        label20MousePressed(e);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        label20MouseReleased(e);
                    }
                });
                panel6.add(label20);

                //---- label21 ----
                label21.setText("\u8868\u60c5");
                label21.setHorizontalAlignment(SwingConstants.CENTER);
                label21.setBorder(new BevelBorder(BevelBorder.RAISED));
                label21.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label21.setOpaque(true);
                label21.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        label21MouseClicked(e);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        label21MousePressed(e);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        label21MouseReleased(e);
                    }
                });
                panel6.add(label21);

                //---- label22 ----
                label22.setText("\u804a\u5929\u8bb0\u5f55");
                label22.setHorizontalAlignment(SwingConstants.CENTER);
                label22.setBorder(new BevelBorder(BevelBorder.RAISED));
                label22.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label22.setOpaque(true);
                label22.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        label22MouseClicked(e);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        label22MousePressed(e);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        label22MouseReleased(e);
                    }
                });
                panel6.add(label22);

                //---- label23 ----
                label23.setText("\u4e2a\u4eba\u4fe1\u606f");
                label23.setHorizontalAlignment(SwingConstants.CENTER);
                label23.setBorder(new BevelBorder(BevelBorder.RAISED));
                label23.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                label23.setOpaque(true);
                label23.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        label23MouseClicked(e);
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        label23MousePressed(e);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        label23MouseReleased(e);
                    }
                });
                panel6.add(label23);
            }
            panel4.add(panel6, BorderLayout.CENTER);
        }
        contentPane.add(panel4, BorderLayout.EAST);

        //======== panel5 ========
        {
            panel5.setMinimumSize(new Dimension(640, 400));
            panel5.setPreferredSize(new Dimension(640, 400));
            panel5.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setMinimumSize(new Dimension(640, 400));
                scrollPane1.setPreferredSize(new Dimension(640, 400));
                scrollPane1.setAutoscrolls(true);

                //---- textArea1 ----
                textArea1.setMinimumSize(new Dimension(640, 400));
                textArea1.setPreferredSize(new Dimension(640, 400));
                textArea1.setBackground(new Color(248, 248, 248));
                textArea1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
                textArea1.setEnabled(false);
                textArea1.setDisabledTextColor(Color.black);
                scrollPane1.setViewportView(textArea1);
            }
            panel5.add(scrollPane1);
        }
        contentPane.add(panel5, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JPanel panel1;
    private JLabel label8;
    private JPanel panel2;
    private JPanel panel3;
    private JScrollPane scrollPane2;
    private JTextArea textArea2;
    private JPanel panel7;
    private JPanel panel8;
    private JPanel panel9;
    private JPanel panel10;
    private JScrollPane scrollPane3;
    private JList list1;
    private JPanel panel4;
    private JPanel panel6;
    private JLabel label24;
    private JLabel label20;
    private JLabel label21;
    private JLabel label22;
    private JLabel label23;
    private JPanel panel5;
    private JScrollPane scrollPane1;
    private JTextArea textArea1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
